from fastapi import UploadFile
from tortoise.expressions import Q

from client import CosClient
from utils import file_utils, utils, excel_utils
from service import BaseService
from models import DataTag


class DataTagService(BaseService):

    @classmethod
    async def get_tag_uid(cls, current_user, tag_name) -> str:
        # search file exist
        dt = await DataTag.get_or_none(Q(e_id=current_user.e_id),
                                       Q(name=tag_name),
                                       Q(is_delete=0))
        if dt is not None:
            return dt.uid

        data_dict = {"name": tag_name}
        uid = utils.get_uid()

        data_dict.update(create_user=current_user.user_id,
                         update_user=current_user.user_id,
                         uid=uid,
                         e_id=current_user.e_id,
                         tenant_id=current_user.tenant_id)
        await DataTag(**data_dict).save()
        return uid

    @classmethod
    async def list_tag(cls, current_user) -> list:
        # search file exist

        dt = await DataTag.filter(Q(is_delete=0), Q(e_id=current_user.e_id))

        return [await item.list_to_dict() for item in dt]
